package com.googlecode.monitoring.service;

import com.googlecode.monitoring.dao.StatisticDao;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.model.StateName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: Sergiy Doroshenko
 * Date: Sep 17, 2010
 * Time: 1:01:48 PM
 */
@Service
public class CheckedStateOfItem implements StateOfItem {
    private StateName stateName = com.googlecode.monitoring.model.StateName.CHECKED;
    private DefaultEmailService emailService;
    private StatisticDao statisticDao;
    private MonitoringService monitoringService;

    @Autowired
    public void setEmailService(DefaultEmailService emailService) {
        this.emailService = emailService;
    }

    @Autowired
    public void setStatisticDao(StatisticDao statisticDao) {
        this.statisticDao = statisticDao;
    }

    @Autowired
    public void setMonitoringService(MonitoringService monitoringService) {
        this.monitoringService = monitoringService;
    }
   
    public StateName getStateName() {
        return stateName;
    }

    @Override
    public void processItem(ItemToCheck item) {
        boolean statusCheck = monitoringService.checkIsItemOutdated(item);
        statisticDao.incrementTotalStateChecks(item.getId());
        if (statusCheck) {
            emailService.sendAlertEmail(item);
            item.switchState(com.googlecode.monitoring.model.StateName.EMAIL_SENT);
            statisticDao.incrementTotalStateDownChecks(item.getId());
        }
    }

}
